package com.example.yangyinzhaixian;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.yangyinzhaixian.Fragemt.CourseFragment;
import com.example.yangyinzhaixian.Fragemt.MineFragment;
import com.example.yangyinzhaixian.Fragemt.NewsFragment;
import com.example.yangyinzhaixian.Fragemt.StudyFragment;
import com.example.yangyinzhaixian.uitl.SharedPreferencesUtils;

import java.util.ArrayList;

public class CourseActivity extends AppCompatActivity implements View.OnClickListener, RadioGroup.OnCheckedChangeListener {
    private int i = 0;
    private ImageView course_image;
    private int[] image = new int[]{R.drawable.help1, R.drawable.help2, R.drawable.help3, R.drawable.help4};
    private ArrayList<Fragment> fragments;
    private FragmentManager supportFragmentManager;
    private FrameLayout course_main_frame;
    private RadioGroup radiogroup;
    private RadioButton[] radioButtons;
    private Fragment mContent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_course);
        initView();
        String frist = (String) SharedPreferencesUtils.getParam(this, "frist", "");
        if (frist.equals("frist"))
        {
            course_image.setVisibility(View.GONE);
        }
        SharedPreferencesUtils.setParam(this,"frist","frist");
        fragments = new ArrayList<>();
        fragments.add(new CourseFragment());
        fragments.add(new MineFragment());
        fragments.add(new NewsFragment());
        fragments.add(new StudyFragment());
        supportFragmentManager = getSupportFragmentManager();
        radioButtons=new RadioButton[radiogroup.getChildCount()];
        for (int i1 = 0; i1 < radioButtons.length; i1++) {
            radioButtons[i1]= (RadioButton) radiogroup.getChildAt(i1);
        }
        radioButtons[0].setChecked(true);
        mContent=fragments.get(0);
        FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.course_main_frame,fragments.get(0));
        fragmentTransaction.commit();
        radiogroup.setOnCheckedChangeListener(this);


    }

    private void initView() {
        course_image = (ImageView) findViewById(R.id.course_image);
        course_image.setImageResource(image[i]);
        course_image.setOnClickListener(this);
        course_main_frame = (FrameLayout) findViewById(R.id.course_main_frame);
        radiogroup = (RadioGroup) findViewById(R.id.radiogroup);
    }

    @Override
    public void onClick(View view) {
        i++;
        if (i <= image.length - 1) {
            course_image.setImageResource(image[i]);
        } else {
            course_image.setVisibility(View.GONE);
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int id) {
        for (int i1 = 0; i1 < radioButtons.length; i1++) {
            if (radioButtons[i1].getId()== id)
            {
                FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
                if (mContent != fragments.get(i1)) {
                    if (!fragments.get(i1).isAdded()) { // 先判断是否被add过
                        fragmentTransaction.hide(mContent).add(R.id.course_main_frame, fragments.get(i1)).commit(); // 隐藏当前的fragment，add下一个到Activity中
                    } else {
                        fragmentTransaction.hide(mContent).show(fragments.get(i1)).commit(); // 隐藏当前的fragment，显示下一个
                    }
                    mContent = fragments.get(i1);
                }
            }
        }
    }
}
